package cn.tedu.erp.project.service.impl;


import cn.tedu.erp.project.ex.ServiceException;
import cn.tedu.erp.project.mapper.PurchaseApplicationMapper;
import cn.tedu.erp.project.pojo.dto.PurchaseApplicationAddNewDTO;
import cn.tedu.erp.project.pojo.dto.PurchaseApplicationUpdateDTO;
import cn.tedu.erp.project.pojo.entity.PurchaseApplication;
import cn.tedu.erp.project.pojo.vo.PurchaseApplicationListVO;
import cn.tedu.erp.project.pojo.vo.PurchaseApplicationStandardVO;
import cn.tedu.erp.project.service.IPurchaseApplicationService;
import cn.tedu.erp.project.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class PurchaseApplicationServiceImpl implements IPurchaseApplicationService {
    @Autowired
    PurchaseApplicationMapper mapper;
    @Override
    public void addNew(PurchaseApplicationAddNewDTO purchaseApplicationAddNewDTO) {
        log.debug("开始处理【添加采购申请】的业务，参数：{}", purchaseApplicationAddNewDTO);
        // 调用参数对象的getName()得到尝试添加的采购申请的名称
        String name = purchaseApplicationAddNewDTO.getCompanyName();
        // 调用Mapper对象的countByName()执行统计查询
        int count = mapper.countByName(name);
        log.debug("根据名称【{}】统计数量，结果：{}", name, count);
        // 判断统计结果是否大于0
        if (count > 0) {
            // 是：名称被占用，抛出异常
            String message = "添加采购申请失败，采购申请名称已经被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }

        // 创建PurchaseApplication对象
        PurchaseApplication purchaseApplication = new PurchaseApplication();
        // 调用BeanUtils.copyProperties(源,目标)将参数对象中的属性复制到PurchaseApplication对象中
        BeanUtils.copyProperties(purchaseApplicationAddNewDTO, purchaseApplication);
        // 调用Mapper对象的insert()执行插入采购申请数据
        log.debug("即将执行插入数据，参数：{}", purchaseApplication);
        mapper.insert(purchaseApplication);
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理【根据ID删除采购申请】的业务，参数：{}", id);
        // 调用Mapper对象的getStandardById()执行查询
        PurchaseApplicationStandardVO purchaseApplication = mapper.getStandardById(id);
        log.debug("根据ID={}检查采购申请数据是否存在，查询结果：{}", id, purchaseApplication);
        // 判断查询结果是否为null
        if (purchaseApplication == null) {
            // 是：数据不存在，抛出异常
            String message = "删除采购申请失败，尝试删除的采购申请数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        // 调用Mapper对象的deleteById()方法执行删除
        log.debug("即将执行删除，参数：{}", id);
        mapper.deleteById(id);
    }

    @Override
    public void updateInfoById(Long id, PurchaseApplicationUpdateDTO purchaseApplicationUpdateDTO) {
        log.debug("开始处理【修改采购申请详情】的业务，ID：{}，新数据：{}", id, purchaseApplicationUpdateDTO);
        // 调用Mapper对象的getStandardById()执行查询
        PurchaseApplicationStandardVO queryResult = mapper.getStandardById(id);
        // 判断查询结果是否为null
        if (queryResult == null) {
            // 是：抛出异常
            String message = "修改采购申请详情失败，尝试修改的采购申请数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        // 调用Mapper对象的countByNameAndNotId()执行统计
        int count = mapper.countByNameAndNotId(id, purchaseApplicationUpdateDTO.getCompanyName());
        // 判断统计结果是否大于0
        if (count > 0) {
            // 是：名称被占用，抛出异常
            String message = "修改采购申请详情失败，采购申请名称已经被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }

        // 创建PurchaseApplication对象
        PurchaseApplication purchaseApplication = new PurchaseApplication();
        // 复制属性，设置ID
        BeanUtils.copyProperties(purchaseApplicationUpdateDTO, purchaseApplication);
        purchaseApplication.setId(id);
        // 调用Mapper对象的update()方法执行修改
        mapper.update(purchaseApplication);
    }

    @Override
    public PurchaseApplicationStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据ID查询采购申请详情】的业务，参数：{}", id);
        // PurchaseApplicationStandardVO queryResult = mapper.getStandardById(id);
        PurchaseApplicationStandardVO queryResult = mapper.getStandardById(id);
        if (queryResult == null) {
            // 是：抛出异常
            String message = "查询采购申请详情失败，采购申请数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return queryResult;
    }

    @Override
    public List<PurchaseApplicationListVO> list() {
        log.debug("开始处理【查询采购申请列表】的业务，无参数");
        // List<PurchaseApplicationListItemVO> list = mapper.list();
        List<PurchaseApplicationListVO> list = mapper.list();
        return list;
    }

    @Override
    public void rebuildCache() {

    }
}
